package ${package.Controller};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import java.util.List;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器 GUN
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    @Autowired
    private ${table.serviceName} ${"${table.serviceName}"?uncap_first};

    @RequestMapping("/getById")
    public ${entity} getById(@RequestParam("id") Long id) {
        if (null == id) {
            return null;
        }
        return ${"${table.serviceName}"?uncap_first}.getById(id);
    }

    @RequestMapping("/save")
    public boolean save(@RequestBody ${entity} ${"${entity}"?uncap_first}) {
        if (null == ${"${entity}"?uncap_first}) {
            throw new RuntimeException("数据为空，插入失败");
        }
        return ${"${table.serviceName}"?uncap_first}.save(${"${entity}"?uncap_first});
    }

    @RequestMapping("/delete")
    public boolean delete(@RequestParam("id") Long id) {
        if (null == id) {
          throw new RuntimeException("数据为空，删除失败");
        }
        return ${"${table.serviceName}"?uncap_first}.removeById(id);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody ${entity} ${"${entity}"?uncap_first}) {
        if (null == ${"${entity}"?uncap_first}) {
           throw new RuntimeException("数据为空，修改失败");
        }
        return ${"${table.serviceName}"?uncap_first}.updateById(${"${entity}"?uncap_first});
    }

    @RequestMapping("/listByPage")
    public PageInfo<${entity}> listByPage(@RequestBody ${entity} ${"${entity}"?uncap_first}) {
        int pageNum = 1;
        int pageSize = 10;
        if (null != ${"${entity}"?uncap_first} && ${"${entity}"?uncap_first}.getPageNum() != null) {
            pageNum = ${"${entity}"?uncap_first}.getPageNum();
        }
        if (null != ${"${entity}"?uncap_first} && ${"${entity}"?uncap_first}.getPageSize() != null) {
           pageSize = ${"${entity}"?uncap_first}.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<${entity}>(${"${table.serviceName}"?uncap_first}.list());
    }

    @RequestMapping("/listAll")
    public List<${entity}> listAll() {
        return ${"${table.serviceName}"?uncap_first}.list();
    }
}
</#if>